package cn.jsj.hzu.qm.facecloud.ciuser.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.hzu.jsj.utils.BeanFactory;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Cilab;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Ciuser;
import cn.jsj.hzu.qm.facecloud.ciuser.service.CilabService;
import cn.jsj.hzu.qm.facecloud.ciuser.service.VCiciService;
import cn.jsj.hzu.qm.facecloud.ciuser.vo.VCici;

/**
 * Servlet implementation class VCiciServlet
 */
public class VCiciServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;encoding=UTF-8");
		
		/* 允许跨域的主机地址 */
		response.setHeader("Access-Control-Allow-Origin", "*");  
		/* 允许跨域的请求方法GET, POST, HEAD 等 */
		response.setHeader("Access-Control-Allow-Methods", "*");  
		/* 重新预检验跨域的缓存时间 (s) */
		response.setHeader("Access-Control-Max-Age", "3600");  
		/* 允许跨域的请求头 */
		response.setHeader("Access-Control-Allow-Headers", "*");  
		/* 是否携带cookie */
		response.setHeader("Access-Control-Allow-Credentials", "true");


		PrintWriter out = response.getWriter();

		String modeString = request.getParameter("mode");
		if (modeString == null) {
			response.getWriter().write("no mode");
			return;
		}
		
		
//		//针对未登录用户的非法访问
		Ciuser ciuser = (Ciuser) request.getSession().getAttribute("ciuser");
//		if (ciuser==null|| ciuser.getType() == 0) {
//			response.sendError(566, "没有登录");
//			return;
//		}

		int mode;
		mode = Integer.parseInt(modeString);
		
		VCiciService vCiciService = BeanFactory.getBean("vCiciService");
		CilabService cilabService = BeanFactory.getBean("cilabService");
		
		switch (mode) {
		/**
		 * 查询所有的签到记录
		 * */
		case 41:{
			List<VCici> vCicis = vCiciService.getAllVCicis();
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append('[');
			for(VCici myVCici : vCicis) {
				stringBuilder.append(myVCici.toJsonString());
				stringBuilder.append(',');
			}
			stringBuilder.append(']');
			out.write(stringBuilder.toString());
			return;
		}
		
		/**
		 * 分页查询所有的签到记录
		 * */
		case 42:{
			String pageString = request.getParameter("page");
			String perpageString = request.getParameter("perpage");
			
			if (pageString == null||perpageString == null) {
				response.sendError(567, "参数缺失");
				return;
			}
			
			int sum = vCiciService.getAllVCiciNum();
			int page = Integer.parseInt(pageString);
			int perpage = Integer.parseInt(perpageString);
			
			List<VCici> vCicis = null;
			
			
			String cilabNameString = ciuser.getMemo();
			Cilab cilab = cilabService.findCilabByName(cilabNameString);
			
			if(ciuser.getType() == 1) {
				vCicis = vCiciService.getAllVCicisByPage(page, perpage, sum);
			}
			
			else if (ciuser.getType() == 2) {
				vCicis = vCiciService.getAllVCicisByPageAndLabid(page, perpage, sum, cilab.getId());
			}
			
			 
			
			//当没有任何记录时，返回空
			if (vCicis == null) {
				VCici vCici = new VCici();
				vCici.setCiin("");
				vCici.setCiout("");
				vCici.setCiname("没有查询到记录");
				vCici.setLabname(" no record");
				vCici.setLablocation("");
				out.write("[" + vCici.toJsonString() + "]");
				return;
			}
			else {
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append('[');
				for(VCici myVCici : vCicis) {
					stringBuilder.append(myVCici.toJsonString());
					stringBuilder.append(',');
				}
				stringBuilder.append(']');
				out.write(stringBuilder.toString());
				return;
			}
			
			
		}
		
		/*
		 * 查询签到总数
		 * */
		case 43:{
			
			
			out.write("["+vCiciService.getAllVCiciNum()+"]");
			return;
		}
		
		/**
		 * 查询某实验室的签到总数
		 * */
		
		case 44:{
			String labidString = request.getParameter("labid");
			if(labidString == null) {
				response.sendError(567, "参数缺失");
				return;
			}
			else {
				int labid = Integer.parseInt(labidString);
				out.write("["+vCiciService.getAllVCiciNum(labid)+"]");
				return;
			}
			
		}
		
		
		
		
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
